using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BattleBase : MonoBehaviour
{
    [SerializeField] protected GameObject fx_GunFire;
    [SerializeField] protected GameObject bullet;
    [SerializeField] protected GameObject bulletPoint;
    [SerializeField] protected GameObject unit;
    [SerializeField] protected int ATK = 10;
    [SerializeField] protected float fireCD = 0.1f;
    protected float _fireCD = 0f;
    [SerializeField] protected int capacity = 7;
    protected int _capacity;
    [SerializeField] protected float reloadCD = 1f;
    protected float _reloadCD = 0f;
    // Start is called before the first frame update
    void Start()
    {
        _capacity = capacity;
    }

    // Update is called once per frame
    void Update()
    {

    }

    protected void Fire()
    {
        if (_capacity > 0)
        {
            fx_GunFire.SetActive(true);
            GameObject _bullet = Instantiate(bullet, bulletPoint.transform.position, Quaternion.Euler(unit.transform.eulerAngles));
            _bullet.GetComponent<Bullet>().InitBullet(ATK, this.gameObject);
            _fireCD = fireCD;
            _capacity--;
        }
        else
        {
            Reload();
        }
    }

    protected IEnumerator Reload()
    {
        _reloadCD = reloadCD;
        // play reload animation
        yield return null;
        for(_reloadCD = reloadCD; _reloadCD>0f; _reloadCD -= Time.deltaTime)
        {
            yield return null;
        }
        _capacity = capacity;
    }
}
